﻿using MS.Entities;
using MS.Enums.Common;
using MS.Identity;
using MS.Utility.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace MS.Controls
{
    public static partial class MSHtml
    {
        public static MvcHtmlString Menu()
        {
            StringBuilder html = new StringBuilder();
            string currentUrl = UserHelper.GetRequestUrl().ToLower();
            html.Append("<ul class='nav-drawer'>");
            html.Append("<li class='nav-item active'><a class='multitabs' href='/BMS'><i class='mdi mdi-home'></i> <span>后台首页</span></a></li>");

            LoginUser loginUser = SecurityIdentity.GetLoginUser();

            List<MS_Base_Function> menuList = new List<MS_Base_Function>();
            if (loginUser.FunctionList != null)
            {
                menuList = loginUser.FunctionList.FindAll(x => x.Type == (int)FunctionType.Menu);
            }

            List<MS_Base_Function> list = menuList.FindAll(x => x.Pid == 0);
            foreach (MS_Base_Function menu in list)
            {
                bool isCheck = false;
                StringBuilder childHtml = new StringBuilder();
                isCheck = MenuBuilder(menu, menuList, childHtml, currentUrl);
                html.Append("<li class='nav-item " + (childHtml.Length > 0 ? "nav-item-has-subnav" : "") + " " + (isCheck ? "open" : "") + "'>");
                if (childHtml.Length > 0)
                {
                    html.AppendFormat("<a href='javascript:void(0)'><i class='mdi {0}'></i> <span>{1}</span></a>", menu.IconClassName, menu.Name);
                }
                else
                {
                    html.AppendFormat("<a href='{0}'><i class='mdi {1}'></i> {2}</a>",menu.Url, menu.IconClassName, menu.Name);
                }
                if (childHtml.Length > 0)
                {
                    html.Append("<ul class='nav nav-subnav' " + (isCheck ? "style='display:block;'" : "") + " >");
                    html.Append(childHtml.ToString());
                    html.Append("</ul>");
                }
            }
            return new MvcHtmlString(html.ToString());
        }

        private static bool MenuBuilder(MS_Base_Function menu, List<MS_Base_Function> menuList, StringBuilder childHtml, string currentUrl)
        {
            bool b = false;
            List<MS_Base_Function> childList = menuList.FindAll(x => x.Pid == menu.Id);
            foreach (MS_Base_Function child in childList)
            {
                bool isCheck = false;

                StringBuilder c = new StringBuilder();
                if (menuList.FindAll(x => x.Pid == child.Id).Count == 0)
                {
                    if (!string.IsNullOrEmpty(child.Url))
                    {
                        isCheck = currentUrl.Contains(child.Url.ToLower());
                    }
                }
                else
                {
                    isCheck = MenuBuilder(child, menuList, c, currentUrl);
                }
                if (!b)
                {
                    b = isCheck;
                }

                if (c.Length > 0)
                {
                    childHtml.Append("<li class='nav-item " + (c.Length > 0 ? "nav-item-has-subnav" : "") + " " + (isCheck ? "open" : "") + "'>");
                    childHtml.AppendFormat("<a href='javascript:void(0)'><i class='mdi {1}'></i> <span>{2}</span></a>", child.Url, child.IconClassName, child.Name);
                }
                else
                {
                    childHtml.Append("<li class='" + (isCheck ? "active" : "") + "'>");
                    childHtml.AppendFormat("<a href='{0}'><i class='mdi {1}'></i> {2}</a>", child.Url, child.IconClassName, child.Name);
                }
                if (c.Length > 0)
                {
                    childHtml.Append("<ul class='nav nav-subnav' " + (isCheck ? "style='display:block;'" : "") + " >");
                    childHtml.Append(c.ToString());
                    childHtml.Append("</ul>");
                }
            }
            return b;
        }

    }
}
